local DB = require("common.DB")
local LuaMongo = _G.lua_mongo
local Human = require("core.Human")
local InnerDataDefine = require("innerdata.Define")
local InnerDataManager = require("innerdata.InnerDataManagerFW")
local Log = require("common.Log")
local RoleDBLogic = require("role.RoleDBLogic")
local MailLogicFW = require("mail.MailLogicFW")
local Lang = require("common.Lang")
local PfLogic = require("tencentApi.PfLogic")
local OperateActChongzhiRank = require("activity.operate.ChongzhiRank")
local ComplexSrvActChongzhiRank = require("activity.complex.ChongzhiRank")
local DateActChongzhiXBackY = require("activity.date.ChongzhiXBackY")
local MiddlePay = require("middle.MiddlePay")


PAY_ERR_OK   = 0
PAY_ERR_REPEAT_ORDER    = 1
PAY_ERR_NO_ACCOUNT      = 2

-- 初始化未领取充值记录
NoFinishPayRecord = NoFinishPayRecord or {}
function init()
    LuaMongo.find(DB.db_pay, {status = 0})
    while true do
        local tmp = {}
        if not LuaMongo.next(DB.db_pay, tmp) then
            break
        end
        NoFinishPayRecord[tmp.order_id] = tmp
    end
end
local fields = {name = 1, lv = 1, rmb = 1}
function pay(account, order_id, rmb, yb, time, gm)
    if not RoleDBLogic.getUuidByAccount(account) then  --帐号不存在
        return PAY_ERR_NO_ACCOUNT       
    end
    
    LuaMongo.find(DB.db_pay, {order_id = order_id})
    local data
    if LuaMongo.next(data) then  -- 重复的订单号
        return PAY_ERR_REPEAT_ORDER
    end
    local record = {
        account = account,
        order_id = order_id,
        rmb = rmb,
        yb = yb,
        time = time,
        status = 0,
    }
    NoFinishPayRecord[record.order_id] = record
    LuaMongo.insert(DB.db_pay, record)
    local human = Human.onlineAccount[account]

    local uuid,name,lv,total
    if human and human.fd then
          InnerDataManager.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_PAY_W, record)     
          uuid = human._id
          name = human.name
          lv = human.db.lv
          total = human.db.rmb
    else
          local db = RoleDBLogic.getDbByAccount(account, fields)  
          uuid = db._id
          name = db.name
          lv   = db.lv  
          total = db.rmb
    end
    if not gm and PfLogic.isAgentOther() then
        Log.write(Log.LOGID_OSS_PAY,uuid,account,name,lv,order_id,rmb, yb, total)
    end    
    MailLogicFW.addSystemMessage(uuid, Lang.PAY_MAIL_TITLE, string.format(Lang.PAY_MAIL_CONTENT, yb))
    OperateActChongzhiRank.onRecharge(uuid, name, yb)
    ComplexSrvActChongzhiRank.onRecharge(uuid, name, yb)
    DateActChongzhiXBackY.onRecharge(uuid, name, yb)
    MiddlePay.sendPayRecord(uuid, rmb, yb)
    return PAY_ERR_OK
end

function payCB(human, type, data)
    local record = NoFinishPayRecord[data.order_id]
    if not record then
        return
    end
    record.status = 1
    LuaMongo.update(DB.db_pay, {_id= data._id}, record)
    NoFinishPayRecord[data.order_id] = nil    
    Human.save(human)
end

function onLogin(human)
    for k,v in pairs(NoFinishPayRecord) do
        if v.account == human.account then
            InnerDataManager.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_PAY_W, v)        
        end
    end
end
InnerDataManager.Register(InnerDataDefine.INNERDATA_TYPE_PAY_L,  payCB)
